package it.xiuxian.lib.utils

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import it.xiuxian.lib.base.App

class ImageUtil {

    companion object{
        private val optionsRound = RequestOptions().format(DecodeFormat.PREFER_ARGB_8888).transform(GlideRoundTransformCenterCrop(20))
        private val optionsNormal = RequestOptions()
        private val optionsCircle= RequestOptions().circleCrop()
        private val optionsDis= RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)

        /**
         * 设置圆形图片
         *
         * @param url
         * @param imageView
         */
        fun setCircleImage(url: Any?, imageView: ImageView?) {
            Glide.with(App.INSTANCE!!).load(url).apply(ImageUtil.optionsCircle)
                .into(imageView!!)
        }

        /**
         * 返回圆形图片Drawable
         *
         * @param url
         * @param drawableBack
         */
        fun setCircleImageReady(url: Any?, drawableBack: DrawableBack) {
            Glide.with(App.INSTANCE!!).load(url).apply(ImageUtil.optionsCircle)
                .into(object : SimpleTarget<Drawable?>() {
                    override fun onResourceReady(
                        resource: Drawable,
                        transition: Transition<in Drawable?>?
                    ) {
                        drawableBack.back(resource)
                    }
                })
        }

        /**
         * 设置图片
         *
         * @param url
         * @param imageView
         */
        fun setImage(url: Any?, imageView: ImageView?, flag: Boolean) {
            Glide.with(App.INSTANCE!!).load(url).apply(ImageUtil.optionsDis)
                .into(imageView!!)
        }

        /**
         * 设置图片
         *
         * @param url
         * @param imageView
         */
        fun setImage(url: Any?, imageView: ImageView?) {
            Glide.with(App.INSTANCE!!).load(url).fitCenter().apply(ImageUtil.optionsNormal)
                .into(imageView!!)
        }

        /**
         * 设置图片
         *
         * @param url
         * @param imageView
         */
        fun setOriginImage(url: Any?, imageView: ImageView?) {
            Glide.with(App.INSTANCE!!).load(url).apply(RequestOptions()).into(imageView!!)
        }

        /**
         * 设置圆角矩形图片
         *
         * @param url
         * @param imageView
         */
        fun setRoundImage(url: Any?, imageView: ImageView?) {
            Glide.with(App.INSTANCE!!).load(url).apply(ImageUtil.optionsRound)
                .into(imageView!!)
        }

//    /**
//     * 模糊图片
//     * @param url
//     * @param imageView
//     * @param radius
//     */
//    public static void flurImage(Object url, ImageView imageView, int radius) {
//        Glide.with(App.INSTANCE!!).load(url)
//                .apply(bitmapTransform(new BlurTransformation(radius)))
//                .into(imageView);
//    }

        //    /**
        //     * 模糊图片
        //     * @param url
        //     * @param imageView
        //     * @param radius
        //     */
        //    public static void flurImage(Object url, ImageView imageView, int radius) {
        //        Glide.with(App.INSTANCE!!).load(url)
        //                .apply(bitmapTransform(new BlurTransformation(radius)))
        //                .into(imageView);
        //    }
        /**
         * 返回圆形图片Bitmap
         *
         * @param url
         * @param bitmapBack
         */
        fun bitmapCircle(url: Any?, bitmapBack: BitmapBack) {
            Glide.with(App.INSTANCE!!)
                .asBitmap()
                .load(url)
                .apply(ImageUtil.optionsCircle)
                .into(object : SimpleTarget<Bitmap?>() {

                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap?>?
                    ) {
                        bitmapBack.back(resource)
                    }
                })
        }

        /**
         * 返回圆形图片Bitmap
         *
         * @param url
         * @param bitmapBack
         */
        fun bitmap(url: Any?, bitmapBack: BitmapBack) {
            Glide.with(App.INSTANCE!!)
                .asBitmap()
                .load(url)
                .apply(ImageUtil.optionsNormal)
                .into(object : SimpleTarget<Bitmap?>() {

                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap?>?
                    ) {
                        bitmapBack.back(resource)
                    }
                })
        }

        interface BitmapBack {
            fun back(bitmap: Bitmap?)
        }

        interface DrawableBack {
            fun back(drawable: Drawable?)
        }
    }
}